//
//  VOCWordViewController.m
//  iVoc
//
//  Created by Wallace Wu on 09-3-19.
//  Copyright 2009 Sonic Solutions. All rights reserved.
//

#import "VOCWordViewController.h"
#import "VOCMutableWordList.h"
#import "VOCSingleListViewController.h"
#import "VOCWordList.h"
#import "UIViewController+(iVOCAddtion).h"

@implementation VOCWordViewController

@synthesize singleWord;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

-(void)switchButton
{
    if(![[VOCMutableWordList favoriteList] containsWord:singleWord])
    {   
        UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(toggleFavorite:)];
        self.navigationItem.rightBarButtonItem = btnAdd;
        [btnAdd release];
    }
    else
    {
        UIBarButtonItem *btnRm = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(toggleFavorite:)];
        self.navigationItem.rightBarButtonItem = btnRm;
        [btnRm release];
    }
}

-(void)reloadData
{
	// build the content
	self.navigationItem.title = self.singleWord.word;
	wordLabel.text = self.singleWord.word;
	
    NSMutableString *expOutput = [[[NSMutableString alloc] init] autorelease];
    //build explanations
    for (VOCWordExp *exp in self.singleWord.explanations) {
        [expOutput appendFormat:@"%@\r\n\r\n%@\r\n\r\n",[NSString stringRepOfFuntion:exp.function],exp.explanation];
    }
    
    expTextView.text = expOutput;
	
	VOCSingleListViewController *listOfThisWordController = (VOCSingleListViewController*)[self prevController];
	
	VOCWordList *currentList = [listOfThisWordController list];
	NSUInteger wordIdx = [[currentList data] indexOfObject:self.singleWord];
	
	[segmentedControl setEnabled:!(wordIdx == [[currentList data] count] -1) forSegmentAtIndex:1];
	[segmentedControl setEnabled:!(wordIdx == 0) forSegmentAtIndex:0];
	
	if(wordIdx == 0)
	{
		[segmentedControl setEnabled:NO forSegmentAtIndex:0];
	}
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // add a button with + letting user can add the word they want into favorite list
    // [self switchButton];
	
	segmentedControl = [[UISegmentedControl alloc] initWithItems:
											[NSArray arrayWithObjects:
											 [UIImage imageNamed:@"arrowup.png"],
											 [UIImage imageNamed:@"arrowdown.png"],
											 nil]];
	[segmentedControl addTarget:self action:@selector(switchWord:) forControlEvents:UIControlEventValueChanged];
	segmentedControl.frame = CGRectMake(0, 0, 90, 30);
	segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
	segmentedControl.momentary = YES;
		
	UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
    [segmentedControl release];
    
	self.navigationItem.rightBarButtonItem = segmentBarItem;
    [segmentBarItem release];
	
	[self reloadData];
}

-(IBAction)switchWord:(id)sender
{
	VOCSingleListViewController *parentViewController = (VOCSingleListViewController*)[self prevController];
	
	VOCWordList *currentList = [parentViewController list];
	NSUInteger wordIdx = [[currentList data] indexOfObject:self.singleWord];
	
	if(segmentedControl.selectedSegmentIndex == 1)
	{
		self.singleWord = [[currentList data] objectAtIndex:wordIdx+1];
	}
	else
	{
		self.singleWord = [[currentList data] objectAtIndex:wordIdx -1];
	}

	[self reloadData];
}

-(void)toggleFavorite:(id)sender
{
    VOCMutableWordList *favorite = [VOCMutableWordList favoriteList];
    
    if(![favorite containsWord:singleWord])
    {
        [favorite addWord:singleWord];  
    }
    else
    {
        [favorite removeWord:singleWord];
    }
    
    [self switchButton];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

- (void)dealloc {
    [wordLabel release];
    [expTextView release];
    [singleWord release];
    [super dealloc];
}


@end
